import time
from selenium.webdriver.common.by import By
from common.Utils import BlogDriver


class BlogList:
    def __init__(self):
        self.url = "http://8.137.19.140:9090/blog_list.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)
        time.sleep(1)  # 等待页面加载

    def ListTestByLogin(self):
        """登录状态下的列表页测试"""
        try:
            # 验证博客标题
            title = self.driver.find_element(
                By.CSS_SELECTOR,
                "body > div.container > div.right > div:nth-child(1) > div.title"
            )
            print(f"博客标题: {title.text}")

            # 验证博客内容
            content = self.driver.find_element(
                By.CSS_SELECTOR,
                "body > div.container > div.right > div:nth-child(1) > div.desc"
            )
            print(f"博客摘要: {content.text[:30]}...")

            # 验证按钮
            button = self.driver.find_element(
                By.CSS_SELECTOR,
                "body > div.container > div.right > div:nth-child(1) > a"
            )
            print(f"按钮文本: {button.text}")

            # 验证个人信息
            nickname = self.driver.find_element(
                By.CSS_SELECTOR,
                "body > div.container > div.left > div > h3"
            )
            print(f"当前用户: {nickname.text}")

            BlogDriver.getScreeShot()
            return True
        except Exception as e:
            print("列表页测试失败:", str(e))
            BlogDriver.getScreeShot()
            return False